package com.dadaban.controller;

import com.dadaban.UserConstant;
import com.dadaban.UserTool;
import com.dadaban.model.UserCar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liu_yan on 2018/9/19.
 */
@RestController
@RequestMapping(value="/userCar")
public class UserCarController {
    private Logger LOGGER = LoggerFactory.getLogger(UserCarController.class);
    @Autowired
    private UserCarRepository userUserCarRepository;

    /**
     * 根据usrUid查询用户车辆信息
     * @return
     */
    @GetMapping(value = "/getUserCars/{usrUid}")
    public Map<String, Object> getUserCarsInfo(@PathVariable("usrUid") String usrUid){
    	LOGGER.info("getUserCarsInfo 根据usrUid查询用户车辆信息");
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	UserCar userCar = new UserCar();
        userCar.setUsrUid(usrUid);
        userCar.setSts("A");
    	Example<UserCar> example = Example.of(userCar);
    	if(userUserCarRepository.findAll(example) == null){
    		resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
        	resultMap.put(UserConstant.ErrorMsg, "根据usrUid查询用户车辆信息出错");
        	return resultMap;
    	}
    	List<UserCar> userCarList = userUserCarRepository.findAll(example);
    	resultMap.put("userCarInfo", userCarList);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
    /**
     * 新增用户车辆信息
     * @return
     */
    @PostMapping(value = "/addUserCar")
    public Map<String, Object> addUserCar(@RequestParam(value = "usrUid", required = true) String usrUid,
    		@RequestParam(value = "carNbr", required = true) String carNbr,
    		@RequestParam(value = "carOrd", required = true) Integer carOrd,
    		@RequestParam(value = "carClr", required = false) String carClr,
    		@RequestParam(value = "carSetNbr", required = false, defaultValue="4") Integer carSetNbr){
        LOGGER.info("addUserCar 新增用户车辆信息");
        /*
         * 必填字段校验
         */
        Map<String, Object> resultMap = new HashMap<String, Object>();
    	resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
    	resultMap.put(UserConstant.ErrorMsg, "新增用户车辆信息出错");
        if(usrUid == null || "".equals(usrUid.trim())){
        	LOGGER.error("addUserCar 新增用户车辆信息出错，缺少用户主键usrUid");
        	return resultMap;
        }
        if(carNbr == null || "".equals(carNbr.trim())){
        	LOGGER.error("addUserCar 新增用户车辆信息出错，缺少车牌号carNbr");
        	return resultMap;
        }
        if(carOrd == null ){
        	LOGGER.error("addUserCar 新增用户车辆信息出错，缺少排序号carOrd");
        	return resultMap;
        }
        
        /*
         * 新增赋值
         */
        UserCar userCar = new UserCar();
        userCar.setUsrUid(usrUid);
        userCar.setCarNbr(carNbr);
        userCar.setCarOrd(carOrd);
        userCar.setCarClr(carClr);
        userCar.setCarSetNbr(carSetNbr);
        userCar.setSts("A");
        Date d = new Date();
        SimpleDateFormat ddf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat tdf = new SimpleDateFormat("kkmmss");
        userCar.setCrtDte(ddf.format(d));
        userCar.setCrtTim(tdf.format(d));
        userUserCarRepository.save(userCar);
        
        resultMap.put("userCar", userCar);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
    /**
     * 根据carUid修改用户车辆信息
     * @return
     */
    @PutMapping(value = "/updateUserCar/{carUid}")
    public Map<String, Object> updateUserCar(@PathVariable("carUid") String carUid,
    		@RequestParam(value = "carNbr", required = false) String carNbr,
    		@RequestParam(value = "carOrd", required = false) Integer carOrd,
    		@RequestParam(value = "carClr", required = false) String carClr,
    		@RequestParam(value = "carSetNbr", required = false) Integer carSetNbr){
    	LOGGER.info("updateUserCar 根据carUid修改用户车辆信息");
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	/*
    	 * 校验
    	 */
    	UserCar userCar = new UserCar();
    	userCar.setCarUid(carUid);
    	userCar.setSts("A");
    	Example<UserCar> example = Example.of(userCar);
    	if(userUserCarRepository.findOne(example).orElse(null) == null){
    		resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
        	resultMap.put(UserConstant.ErrorMsg, "根据carUid修改用户车辆信息出错");
        	return resultMap;
    	}
    	UserCar source = userUserCarRepository.findOne(example).get();
    	
    	/*
    	 * 用户车辆数据更新
    	 */
    	userCar.setCarNbr(carNbr);
    	userCar.setCarOrd(carOrd);
    	userCar.setCarClr(carClr);
    	userCar.setCarSetNbr(carSetNbr);
    	Date d = new Date();
    	SimpleDateFormat ddf = new SimpleDateFormat("yyyyMMdd");
    	SimpleDateFormat tdf = new SimpleDateFormat("kkmmss");
    	userCar.setRecDte(ddf.format(d));
    	userCar.setRecTim(tdf.format(d));
        UserTool.copyNullProperties(source, userCar);
        userUserCarRepository.save(userCar);
        
        resultMap.put("userCar", userCar);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
    /**
     * 根据carUid删除用户车辆信息
     * @return
     */
    @DeleteMapping(value = "/deleteUserCar/{carUid}")
    public Map<String, Object> deleteUserCar(@PathVariable("carUid") String carUid){
    	LOGGER.info("deleteUserCar 根据carUid删除用户车辆信息");
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	/*
    	 * 校验
    	 */
    	UserCar userCar = new UserCar();
    	userCar.setCarUid(carUid);
    	userCar.setSts("A");
    	Example<UserCar> example = Example.of(userCar);
    	if(userUserCarRepository.findOne(example).orElse(null) == null){
    		resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
        	resultMap.put(UserConstant.ErrorMsg, "根据carUid删除用户车辆信息出错");
        	return resultMap;
    	}
    	UserCar source = userUserCarRepository.findOne(example).get();
    	
    	userCar.setSts("C");
    	Date d = new Date();
    	SimpleDateFormat ddf = new SimpleDateFormat("yyyyMMdd");
    	SimpleDateFormat tdf = new SimpleDateFormat("kkmmss");
    	userCar.setRecDte(ddf.format(d));
    	userCar.setRecTim(tdf.format(d));
        UserTool.copyNullProperties(source, userCar);
        userUserCarRepository.save(userCar);
        
        resultMap.put("userCar", userCar);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
}
